package id.brian;

import android.graphics.Canvas;
import android.view.SurfaceHolder;

public class GameThread extends Thread {
	private SurfaceHolder mSurfaceHolder;
	private Panel mPanel;
	private boolean mRun;

	public GameThread(SurfaceHolder surfaceHolder, Panel panel) {
		mSurfaceHolder = surfaceHolder;
		mPanel = panel;
	}

	public void setRunning(boolean run) {
		mRun = run;
	}

	@Override
	public void run() {
		Canvas c;
		while (mRun) {
			c = null;
			try {
				c = mSurfaceHolder.lockCanvas();
				synchronized(mSurfaceHolder) {
					mPanel.updatePhysics();
					mPanel.onDraw(c);
				}
			} finally {
				if (c != null)
					mSurfaceHolder.unlockCanvasAndPost(c);
			}
		}
	}

	public SurfaceHolder getSurfaceHolder() {
		return mSurfaceHolder;
	}
}